package com.yinhe.music.yhmusic.adapter;

import android.view.MenuItem;
import android.widget.CheckBox;
import android.widget.ImageView;

import com.chad.library.adapter.base.BaseItemDraggableAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.yinhe.music.common.GlideHelper.GlideHelper;
import com.yinhe.music.yhmusic.R;
import com.yinhe.music.yhmusic.model.SongMenuList;

import java.util.List;

/**
 * 批量操作歌单列表adapter
 *
 * @author hasee
 * @date 2017/8/24
 */

public class MenuManagerAdapter extends BaseItemDraggableAdapter<SongMenuList, BaseViewHolder> {
    private boolean isSelect;

    public MenuManagerAdapter(List<SongMenuList> data) {
        super(R.layout.fragment_menu_manager, data);
    }

    public void selectAll(MenuItem item) {
        if (isSelect) {
            isSelect = false;
            item.setTitle("全选");
        } else {
            isSelect = true;
            item.setTitle("取消全选");
        }
        for (SongMenuList i : getData()) {
            i.setCheck(isSelect);
        }
        notifyDataSetChanged();
    }

    public void setUnChek() {
        for (SongMenuList i : getData()) {
            i.setCheck(false);
        }
    }

    /**
     * Implement this method and use the helper to adapt the view to the given item.
     *
     * @param helper A fully initialized helper.
     * @param item   The item that needs to be displayed.
     */
    @Override
    protected void convert(final BaseViewHolder helper, final SongMenuList item) {
        helper.setText(R.id.menu_name, item.getSongMenuName())
                .setText(R.id.menu_sum, item.getSongNum() + "首");

        ImageView image = helper.getView(R.id.song_menu_img);
        GlideHelper.setImageResource(image, item.getImage(), R.drawable.default_cover);

        helper.itemView.setOnClickListener((v) -> {
            if (item.isCheck()) {
                item.setCheck(false);
                helper.setChecked(R.id.checkbox, false);
            } else {
                item.setCheck(true);
                helper.setChecked(R.id.checkbox, true);
            }
        });

        CheckBox checkBox = helper.getView(R.id.checkbox);
        checkBox.setOnCheckedChangeListener((compoundButton, b) ->
            item.setCheck(b)
        );
        checkBox.setChecked(item.isCheck());
    }
}
